﻿using LiteBlog.Common.Entities;
using LiteBlog.Common.Enums;
using LiteBlog.Repositories.Common;
using LiteBlog.Repositories.Contracts;
using LiteBlog.Repositories.Framework;
using LiteBlog.Repositories.Helpers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace LiteBlog.Repositories
{
    internal class PageMetaRepository : DataAccess
    {
        internal PageMetaRepository(IDataContext context, IXmlHelper xmlHelper) : base(context, xmlHelper)
        {
            _path = context.GetPath(DataStoreType.Pages, null);
        }

        internal void Create(Page page)
        {
            var root = XElement.Load(_path);
            var pageElem = root.Elements("Page").SingleOrDefault(elem => (string)elem.Attribute("FileId") == page.FileId);
            if (pageElem == null)
            {
                pageElem = new XElement("Page", new XAttribute("FileId", page.FileId),
                                                    new XAttribute("Title", page.Title),
                                                    new XAttribute("Published", page.Published),
                                                    new XAttribute("IncludeInHeaderMenu", page.IncludeInHeaderMenu));
                root.Add(pageElem);
            }
            _xmlHelper.Save(root, _path);
        }

        internal void Update(Page page)
        {
            var root = XElement.Load(_path);
            var pageElem = root.Elements("Page").SingleOrDefault(elem => (string)elem.Attribute("FileId") == page.FileId);
            if (pageElem != null)
            {
                pageElem.SetAttributeValue("Title", page.Title);
                pageElem.SetAttributeValue("Published", page.Published);
                pageElem.SetAttributeValue("IncludeInHeaderMenu", page.IncludeInHeaderMenu);
            }
            _xmlHelper.Save(root, _path);
        }

        internal void Delete(string fileId)
        {
            var root = XElement.Load(_path);
            var pageElem = root.Elements("Page").SingleOrDefault(elem => (string)elem.Attribute("FileId") == fileId);
            if (pageElem != null)
            {
                pageElem.Remove();
            }
            _xmlHelper.Save(root, _path);
        }

        internal List<Page> Get()
        {
            var root = XElement.Load(_path);
            var pages = new List<Page>();
            foreach (var elem in root.Elements("Page"))
            {
                var page = new Page();
                page.Title = (string)elem.Attribute("Title");
                page.FileId = (string)elem.Attribute("FileId");
                page.Published = (bool)elem.Attribute("Published");
                page.IncludeInHeaderMenu = (bool)elem.Attribute("IncludeInHeaderMenu");
                pages.Add(page);
            }
            return pages;
        }

        internal Page Get(string fileId)
        {
            return Get().SingleOrDefault(p => p.FileId == fileId);
        }
    }
}
